package com.aier.admin.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.stereotype.Component;

import com.aier.admin.service.IAdminVideoService;
import com.aier.common.dao.ICommonDAO;
import com.aier.entrance.entity.VideoDevice;
import com.aier.entrance.entity.VideoDeviceRowMapper;

@Component("adminVideoService")
public class AdminVideoServiceImpl_J implements IAdminVideoService {

    @Resource(name = "commonDao")
    private ICommonDAO commonDao;

    public ICommonDAO getCommonDao() {
        return commonDao;
    }

    public void setCommonDao(ICommonDAO commonDao) {
        this.commonDao = commonDao;
    }

    @Override
    public int insertEntrance(VideoDevice videoDevice) {
        VideoDevice vd = new VideoDevice();
        vd.setSn(videoDevice.getSn());
        vd.setState(videoDevice.getState());
        vd.setUser(videoDevice.getUser());
        vd.setPwd(videoDevice.getPwd());
        vd.setUrl(videoDevice.getUrl());
        vd.setSupplier(videoDevice.getSupplier());

        commonDao.insert(VideoDeviceRowMapper.getPreparedStatementCreator(vd),
                new GeneratedKeyHolder());

        return 1;
    }

    // 查询所有视频信息
    @Override
    public Map<String, Object> searchVideoDevice(int page, int page_size) {
        StringBuffer sql = new StringBuffer("select * from video_device");
        List<Object> lo = new ArrayList<Object>();
        return commonDao.queryPage(sql.toString(), page, page_size, lo,
                new VideoDeviceRowMapper());
    }

    // 删除视频信息,逻辑删除
    @Override
    public int updateVideoByid(Long vid) {
        
        if (commonDao.update("update video_device set state=-1 where vid=?",
                new Object[] { vid }) > 0) {
            return 1;
        } else {
            return 0;
        }

    }

    // 修改视频信息
    @Override
    public int updateByvid(Long vid, String sn, Short state, String user,
            String pwd, String url, Short supplier) {

        if (commonDao
                .update("update video_device v set v.sn=?,v.state=?,v.user=?,v.pwd=?,v.url=?,v.supplier=? where v.vid=?",
                        new Object[] { sn, state, user, pwd, url, supplier, vid }) > 0) {
            return 1;
        } else {
            return 0;
        }
    }

    // 根据id查询某条视频记录
    @Override
    public VideoDevice searchByvid(Long vid) {
        return (VideoDevice) commonDao.listByObject(
                "select * from video_device where vid=?", new Object[] { vid },
                new VideoDeviceRowMapper());
    }

    // 查出所有未分配的视频设备
    @Override
    public Map<String, Object> selectUnassignVideo(int page, int page_size) {
        StringBuffer sql = new StringBuffer(
                "select * from video_device where pid is  null");
        List<Object> lo = new ArrayList<Object>();
        return commonDao.queryPage(sql.toString(), page, page_size, lo,
                new VideoDeviceRowMapper());
    }

    // 查出所有已分配的视频设备
    @Override
    public Map<String, Object> selectAllocationVideo(int page, int page_size) {
        StringBuffer sql = new StringBuffer(
                "select * from video_device where pid is not null");
        List<Object> lo = new ArrayList<Object>();
        return commonDao.queryPage(sql.toString(), page, page_size, lo,
                new VideoDeviceRowMapper());
    }

    // 根据id分派视频设备
    @Override
    public int updatePidByVid(Integer pid, Long vid) {
        if (null != pid) {
            if (commonDao.update("update video_device set pid=? where vid=?",
                    new Object[] { pid, vid }) > 0) {
                return 1;
            } else {
                return 0;
            }
        }else {
            if (commonDao.update("update video_device set pid=NULL where vid=?",
                    new Object[] {vid }) > 0) {
                return 1;
            } else {
                return 0;
            }
        }
    }
}
